package mes.kanban.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import mes.kanban.entity.CustomerCompany;
import mes.kanban.respository.CustomerCompanyRepository;
import mes.kanban.service.CustomerCompanyServiceI;

@Service
public class CustomerCompanyServiceImpl implements CustomerCompanyServiceI {
	
	@Autowired
	private CustomerCompanyRepository customerCompanyRepository;

	@Override
	public CustomerCompany findById(String userCode) {
		return this.customerCompanyRepository.findOne(userCode);
	}

	@Override
	@Transactional
	public CustomerCompany findOrSaveById(String userCode) {

		CustomerCompany cc = this.findById(userCode);
		
		if(cc == null) {
			
			cc = new CustomerCompany();
			
			cc.setCompanyCode(userCode);
			cc.setCompanyName(userCode);
			
			this.customerCompanyRepository.save(cc);
		}
		
		return cc;
	}

	@Override
	@Transactional
	public void saveCustomerCompany(CustomerCompany company) {
		this.customerCompanyRepository.save(company);
	}

	@Override
	@Transactional
	public void updateCustomerConpany(CustomerCompany company) {
		this.customerCompanyRepository.saveAndFlush(company);
	}
	
	
	

}
